package com.ricchieri.manage.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.springframework.dao.DataAccessException;

import com.ricchieri.common.BusinessSettings;

@ManagedBean(name = "addPassengerReservationMB")
@ViewScoped
public class AddPassengerReservationManageBean extends AddUserManageBean {

	private static final long serialVersionUID = -3389982664588609204L;
	private static final String DEFAULT_PASSWORD = "1234567";

	public String save() {
		try {
			
			if(validateForm()) {
				this.setDefaultEmail();
				this.setDefaultPhone();
				getUser().setAutorized(Boolean.TRUE);
				getUser().setDocumentType(this.obtainDocumentType(getDocumentType()));
				getUser().setUserEmail(this.getEmails());
				getUser().setUserPhone(this.getPhones());
				
				getUser().setRoles(this.generateRoles(getUser()));
				
				getUser().setPassword(DEFAULT_PASSWORD);
				getUser().setConfirmPassword(DEFAULT_PASSWORD);
				
				getUserService().addUser(getUser());
				return BusinessSettings.SUCCESS;
			} else {
				return BusinessSettings.UNSUCCESS;
			}
			
		} catch (DataAccessException e) {
			return BusinessSettings.UNSUCCESS;
		}
	}
	
	
	@Override
	public List<String> getSelectedRoles() {
		List <String> selectedRoles = new ArrayList<String>();
		selectedRoles.add(BusinessSettings.ROLE_PASSANGER_ID);
		setSelectedRoles(selectedRoles);
		return super.getSelectedRoles();
	}
			

	@Override
	protected String getFormName() {
		return "addPassengerReservationForm";
	}
}

